home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWMemory / FWMemHlp.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  7.4 KB  |  248 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMemHlp.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWMEMHLP_H
  13. #include "FWMemHlp.h"
  14. #endif
  15.  
  16. #ifndef FWMEMMGR_H
  17. #include "FWMemMgr.h"
  18. #endif
  19.  
  20. #ifndef FWPRIDEB_H
  21. #include "FWPriDeb.h"
  22. #endif
  23.  
  24. #ifndef FWODEXCE_H
  25. #include "FWODExce.h"
  26. #endif
  27.  
  28. #ifdef FW_BUILD_MAC
  29. #pragma segment FWMemory
  30. #endif
  31.  
  32. //========================================================================================
  33. //    class FW_CAcquireLockedSystemHandle
  34. //========================================================================================
  35.  
  36. FW_DEFINE_AUTO(FW_CAcquireLockedSystemHandle)
  37.  
  38. //----------------------------------------------------------------------------------------
  39. // FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle
  40. //----------------------------------------------------------------------------------------
  41.  
  42. FW_CAcquireLockedSystemHandle::FW_CAcquireLockedSystemHandle(FW_PlatformHandle aSystemHandle)
  43.                      : fLockedSystemHandle(aSystemHandle) 
  44. {
  45. #ifdef FW_BUILD_MAC
  46.     fLockState = ::HGetState(aSystemHandle);
  47. #endif
  48.     fLockedPointer = FW_CMemoryManager::LockSystemHandle(aSystemHandle);
  49.     FW_END_CONSTRUCTOR
  50. }
  51.  
  52. //----------------------------------------------------------------------------------------
  53. // FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle
  54. //----------------------------------------------------------------------------------------
  55.  
  56. FW_CAcquireLockedSystemHandle::~FW_CAcquireLockedSystemHandle()
  57. {
  58.     FW_START_DESTRUCTOR
  59. #ifdef FW_BUILD_MAC
  60.     ::HSetState(fLockedSystemHandle, fLockState);
  61. #else
  62.     FW_CMemoryManager::UnlockSystemHandle(fLockedSystemHandle);
  63. #endif
  64. }
  65.  
  66. //========================================================================================
  67. // CLASS FW_CAcquireTemporarySystemHandle
  68. //========================================================================================
  69.  
  70. FW_DEFINE_AUTO(FW_CAcquireTemporarySystemHandle)
  71.  
  72. //----------------------------------------------------------------------------------------
  73. // FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle
  74. //----------------------------------------------------------------------------------------
  75.  
  76. FW_CAcquireTemporarySystemHandle::FW_CAcquireTemporarySystemHandle(unsigned long size) :
  77.     fTemporarySystemHandle(FW_CMemoryManager::AllocateSystemHandle(size))
  78. {
  79.     FW_TRY
  80.     {
  81.         fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  82.         fFree = TRUE;
  83.     }
  84.     FW_CATCH_BEGIN
  85.     FW_CATCH_EVERYTHING()
  86.     {
  87.         FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
  88.         FW_THROW_SAME();
  89.     }
  90.     FW_CATCH_END
  91.     
  92.     FW_END_CONSTRUCTOR
  93. }
  94.  
  95. //----------------------------------------------------------------------------------------
  96. // FW_CAcquireTemporarySystemHandle::Orphan
  97. //----------------------------------------------------------------------------------------
  98.  
  99. FW_PlatformHandle FW_CAcquireTemporarySystemHandle::Orphan()
  100. {
  101.     FW_ASSERT(fFree == TRUE);
  102.     fFree = FALSE;
  103.     
  104.     FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  105.     fMemoryPointer = NULL;
  106.  
  107.     return fTemporarySystemHandle;
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle
  112. //----------------------------------------------------------------------------------------
  113.  
  114. FW_CAcquireTemporarySystemHandle::~FW_CAcquireTemporarySystemHandle(void)
  115. {
  116.     FW_START_DESTRUCTOR
  117.     
  118.     if(fFree)
  119.     {
  120.         FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  121.         FW_CMemoryManager::FreeSystemHandle(fTemporarySystemHandle);
  122.     }
  123. }
  124.  
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // FW_CAcquireTemporarySystemHandle::Resize
  128. //----------------------------------------------------------------------------------------
  129.  
  130. void* FW_CAcquireTemporarySystemHandle::Resize(unsigned long newSize)
  131. {
  132.     // unlock the handle before resizing
  133.     FW_CMemoryManager::UnlockSystemHandle(fTemporarySystemHandle);
  134.     
  135.     // then resize it
  136.     FW_TRY
  137.     {
  138.         fTemporarySystemHandle =  FW_CMemoryManager::ResizeSystemHandle(fTemporarySystemHandle, newSize);
  139.         fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  140.     }
  141.     FW_CATCH_BEGIN
  142.     FW_CATCH_EVERYTHING()
  143.     {
  144.         // relock the handle
  145.         fMemoryPointer = FW_CMemoryManager::LockSystemHandle(fTemporarySystemHandle);
  146.         FW_THROW_SAME();
  147.     }
  148.     FW_CATCH_END
  149.     
  150.     return GetPointer();
  151. }
  152.  
  153. //========================================================================================
  154. // CLASS FW_CAcquireTemporaryMemory
  155. //========================================================================================
  156.  
  157. FW_DEFINE_AUTO(FW_CAcquireTemporaryMemory)
  158.  
  159. //----------------------------------------------------------------------------------------
  160. // FW_CAcquireTemporaryMemory::FW_CAcquireTemporaryMemory
  161. //----------------------------------------------------------------------------------------
  162.  
  163. FW_CAcquireTemporaryMemory::FW_CAcquireTemporaryMemory(unsigned long size)
  164. {
  165.     fPointer = FW_CMemoryManager::AllocateBlock(size);
  166.     FW_END_CONSTRUCTOR
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. // FW_CAcquireTemporaryMemory::~FW_CAcquireTemporaryMemory
  171. //----------------------------------------------------------------------------------------
  172.  
  173. FW_CAcquireTemporaryMemory::~FW_CAcquireTemporaryMemory()
  174. {
  175.     FW_START_DESTRUCTOR
  176.     FW_CMemoryManager::FreeBlock(fPointer);
  177. }
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // FW_CAcquireTemporaryMemory::OrphanPointer
  181. //----------------------------------------------------------------------------------------
  182.  
  183. void* FW_CAcquireTemporaryMemory::OrphanPointer()
  184. {
  185.     FW_ASSERT(fPointer != NULL);        // Only orphan once
  186.     void* t = fPointer;
  187.     fPointer = NULL;
  188.     return t;
  189. }
  190.  
  191. #ifdef FW_BUILD_MAC
  192. //========================================================================================
  193. // CLASS FW_CMacAcquireMultiFinderHeapZone
  194. //========================================================================================
  195.  
  196. FW_DEFINE_AUTO(FW_CMacAcquireMultiFinderHeapZone)
  197.  
  198. //----------------------------------------------------------------------------------------
  199. // FW_CMacAcquireMultiFinderHeapZone::FW_CMacAcquireMultiFinderHeapZone
  200. //----------------------------------------------------------------------------------------
  201.  
  202. FW_CMacAcquireMultiFinderHeapZone::FW_CMacAcquireMultiFinderHeapZone() :
  203.     fOldHeap(NULL)
  204. {
  205.     OSErr heapError;
  206.     Handle heapTestHandle = ::TempNewHandle( 1, &heapError );
  207.  
  208.     if (heapTestHandle != 0)
  209.     {
  210.         THz tempHeap = ::HandleZone( heapTestHandle );
  211.         heapError = ::MemError( );
  212.  
  213.         ::DisposeHandle( heapTestHandle );
  214.         if (heapError == noErr)
  215.         {
  216.             if (tempHeap != 0 ) {
  217.                 fOldHeap = ::GetZone( );
  218.                 ::SetZone( tempHeap );
  219.             }
  220.         }
  221.         else
  222.         {
  223.             FW_Failure(heapError);
  224.         }
  225.     }
  226.     else
  227.     {
  228.         FW_Failure(FW_xMemoryExhausted);
  229.     }
  230.  
  231.     FW_END_CONSTRUCTOR
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // FW_CMacAcquireMultiFinderHeapZone::~FW_CMacAcquireMultiFinderHeapZone
  236. //----------------------------------------------------------------------------------------
  237.  
  238. FW_CMacAcquireMultiFinderHeapZone::~FW_CMacAcquireMultiFinderHeapZone()
  239. {
  240.     FW_START_DESTRUCTOR
  241.     
  242.     if ( fOldHeap != NULL )
  243.         ::SetZone( fOldHeap );
  244. }
  245. #endif
  246.  
  247.  
  248.